package com.xlh.server.protocol.support.json;

import android.text.Html;
import android.util.Log;
import android.util.LogPrinter;
import android.view.View;
import android.widget.TextView;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xlh.Utils.FontHandler;
import com.xlh.Utils.GsonManager;

import com.xlh.bean.ProtocolObject.Exits;
import com.xlh.bean.ProtocolObject.LongBoxLB;
import com.xlh.bean.ProtocolObject.ParamObject.LabelParam;
import com.xlh.bean.TitleBean;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mike@Q184377367 on 2018/7/2.
 */

public class ExitsServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "exits";
    private Exits protccoObj;
    //FontHandler fontHandler;
    public ExitsServer(IActivity atv){
        this.atv = atv;
    }

    @Override
    public void takeMessage(String t,String s){

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, Exits.class);

        if(protccoObj==null || protccoObj.getExits().size()<=0) return;



        final ExitsServer me = this;
       // fontHandler = new FontHandler(this.atv.getActivity());



        this.putDirAreaData( protccoObj.getExits());
        this.putOtherExitsData(protccoObj.getExits());

        //消息函数内的内容结束------

        //隐藏用户按钮区
        me.atv.getActivity().gdUserBtnArea.setVisibility(View.GONE);
        me.atv.getActivity().dirArea.setVisibility(View.VISIBLE);


    }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit() {

    }



    public void putOtherExitsData(List<LabelParam> exits){




        Boolean  flag = false;
        for (int i=0;i<exits.size();i++) {

            flag = false;
            for(TextView e : this.atv.getActivity().exitsMap.keySet() ){
                if(this.atv.getActivity().exitsMap.get(e).contains(exits.get(i).getCmd())){
                    flag = true;
                    break;
                }
            }

            if(flag) continue;


            TitleBean tb = new TitleBean();
            tb.setmText(exits.get(i).getLabel());

            tb.setSpanned(new HtmlSpanner(this.atv).fromHtml(exits.get(i).getLabel()));
           // tb.setSpanned(Html.fromHtml(exits.get(i).getLabel(),null,fontHandler));
            tb.setmAction(exits.get(i).getCmd());
            tb.setHeight(30);
            this.atv.getActivity().getOtherExitsViewManager().getDataAdapter().addData(tb);

        }

        this.atv.getActivity().getOtherExitsViewManager().getDataAdapter().update();
    }




    public void putDirAreaData(List<LabelParam> exits){

        for (int i=0;i<exits.size();i++) {
            for(TextView e :  this.atv.getActivity().exitsMap.keySet()){
                if(this.atv.getActivity().exitsMap.get(e).contains(exits.get(i).getCmd())){
                    e.setVisibility(View.VISIBLE);

                    //e.setText(Html.fromHtml(exits.get(i).getLabel(),null,fontHandler));
                    e.setText(this.atv.getActivity().getHtmlSpanner().fromHtml(exits.get(i).getLabel()));
                    e.setTag(exits.get(i).getCmd());
                }
            }

        }
    }




}
